<template>
  <div id="site-message">
    <div class="title-all">查看全部</div>
    <div class="container">
      <!-- 侧边栏 aside -->
      <aside class="c-aside">
        <el-date-picker
          v-model="dataScope"
          type="daterange"
          range-separator="-"
          value-format="yyyy-MM-dd"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          @change="changeData"
        >
        </el-date-picker>
        <el-tabs v-model="activeName" stretch>
          <el-tab-pane
            v-for="panelItem in tabPane"
            :key="panelItem.name"
            :label="panelItem.label"
            :name="panelItem.name"
          >
            <div
              class="site-content"
              :class="{is_active: is_active == readItem.id}"
              v-for="readItem in panelItem.readList"
              :key="readItem.id"
              @click="lookDesc(readItem.id, panelItem.readList)"
            >
              <div class="site-desc-area">
                <div class="site-desc-title">
                  {{ readItem.title }}
                </div>
                <div class="site-desc-context">
                  {{ readItem.content }}
                </div>
              </div>
              <div>
                <el-tag
                  v-if="readItem.level"
                  :type="readItem.level === 2 ? 'danger' : 'warning'"
                  >{{ readItem.level === 2 ? '重要' : '一般' }}</el-tag
                >
              </div>
            </div>
          </el-tab-pane>
        </el-tabs>
      </aside>
      <!-- 内容 main -->
      <main class="c-main">
        <div v-if="dataDesc == null" class="no-message">
          <img src="@/assets/images/noMessage.png" alt="无消息" />
          <div>点击左侧查看消息</div>
        </div>
        <message-desc v-else :data-desc="dataDesc"> </message-desc>
      </main>
    </div>
  </div>
</template>

<script>
import {siteApi} from '@/api'
import MessageDesc from './component/MessageDesc.vue'
export default {
  name: 'SiteMessage',
  components: {
    MessageDesc,
  },
  data() {
    return {
      // 日期区间
      dataScope: '',
      // 默认选中的
      activeName: 'unRead',
      // 面板
      tabPane: [
        {
          label: '未读',
          name: 'unRead',
          readList: [],
        },
        {
          label: '已读',
          name: 'read',
          readList: [],
        },
      ],
      // 所有的站内信消息
      allReadList: [],
      // 选中id
      is_active: '',
      // 数据详情
      dataDesc: null,
    }
  },
  mounted() {
    this.__init__()
  },
  methods: {
    __init__() {
      this.getSiteList()
    },
    /**
     * 获取所有的站内信
     */
    getSiteList() {
      siteApi.getAllSiteMessage().then(res => {
        this.allReadList = res.data
        this.groupReadData(this.allReadList)
      })
    },
    /**
     * 进行已读和未读的分组
     */
    groupReadData(data) {
      // 给数据置空
      this.tabPane[0].readList = [] // 未读
      this.tabPane[1].readList = [] // 已读

      // 数据分组
      data.forEach(item => {
        // 为真就是已读
        if (item.isRead) {
          this.tabPane[1].readList.push(item)
        } else {
          this.tabPane[0].readList.push(item)
        }
      })
    },

    /**
     * 筛选数据
     */
    lookDesc(id, list) {
      this.is_active = id
      // filter
      this.dataDesc = [...list.filter(v => v.id === id)][0]
    },

    changeData(timeArr) {
      // 根据开始时间和结束时间，在所有的数据中筛选出来
      const timeList = this.allReadList.filter(item => {
        return item.createTime >= timeArr[0] && item.createTime <= timeArr[1]
      })
      this.groupReadData(timeList)
    },
  },
}
</script>

<style lang="less" scoped>
#site-message {
  height: 100%;
  .title-all {
    width: 100%;
    height: 50px;
    padding: 10px 20px;
    font-size: 16px;
    font-weight: 600;
    border-bottom: 1px solid @lineColor;
  }
  .container {
    display: flex;
    height: calc(100% - 50px);
  }
  .c-aside {
    width: 250px;
    height: 100%;
    border-right: 1px solid @lineColor;
  }
  // 日期选择器
  .el-date-editor {
    width: 90%;
    left: 50%;
    transform: translateX(-50%);
    margin: 20px 0;
    ::v-deep .el-range-separator {
      padding: 0;
    }
  }
  .el-tabs {
    height: calc(100% - 40px);
    /deep/ .el-tabs__header {
      margin: 0;
    }
    /deep/ .el-tabs__nav-wrap::after {
      height: 1px;
    }
    /deep/ .el-tabs__content {
      height: calc(100% - 100px);
      overflow: auto;
      &::-webkit-scrollbar {
        display: none;
      }
    }
    /deep/ .el-tabs__item {
      height: 50px;
    }
  }
  .site-content {
    display: flex;
    // height: 50px;
    padding: 15px;
    cursor: pointer;
    .site-desc-area {
      width: 80%;
      margin-right: 5px;
    }
    &:hover {
      background-color: #f6f7f9;
    }
  }
  .is_active {
    background-color: #f6f7f9;
  }
  .site-desc-title,
  .site-desc-context {
    width: 100%;
    line-height: 20px;
    font-size: 14px;
    white-space: nowrap;
    text-overflow: ellipsis;
    overflow: hidden;
  }
  .site-desc-context {
    font-size: 13px;
    color: #aaacb0;
  }
  .el-tag {
    height: 25px;
    line-height: 25px;
    padding: 0 5px;
    margin: 0 8px;
    border: 0;
    align-self: center;
    &.el-tag--danger {
      color: #e44c4c;
    }
  }
  .c-main {
    flex: 1;
    height: 100%;
    overflow-y: auto;
    .no-message {
      display: flex;
      height: 100%;
      width: 100%;
      flex-direction: column;
      justify-content: center;
      align-items: center;
      color: #666666;
      font-size: 25px;
      img {
        width: 200px;
      }
    }
  }
}
</style>
